(function(angular) {
	'use strict';
	angular.module('fs.waf').factory('feeTypeService', feeTypeService)
	.filter("prodCodeFilter", [ '$filter', '$locale', 'feeTypeService', 'SYS_PARAMS', function($filter, $locale, feeTypeService, SYS_PARAMS) {
		return function(optionValue, pattern) {
			if("*" == optionValue) {
				var label = optionValue ? (SYS_PARAMS.ALL[optionValue.toUpperCase()] || optionValue) : optionValue;
				return $filter('translate')(label);
			} else {
				//in the list page if db product Code like 'FSCSBK:PROD_CODE1', the Product code in the list only show 'PROD_CODE1'. the siteCode length is 6.
				return optionValue.substring(7, optionValue.length);
			}
			
		};
	} ]);
	feeTypeService.$inject = [ '$q', 'loggerFactory', 'commonService', 'ajaxServiceFactory', 'modalService', 'gridService' ];
	function feeTypeService($q, loggerFactory, commonService, ajaxServiceFactory, modalService, gridService) {
		var log = loggerFactory.getLogger("feeTypeService"), ajaxService = ajaxServiceFactory.buildAjaxService({
			dropDownDataOptions : {
				url : 'do/cda/feeType/dropDownDataOptions',
				method : 'get'
			},
			prodCodeOptions : {
				url : 'do/cda/feeType/prodCodeOptions',
				method : 'post'
			}
		});
		function dropDownDataOptions() {
			var defer = $q.defer();
			ajaxService.dropDownDataOptions().then(function(response) {
				var options = {};
				if (response.data) {
					angular.forEach(response.data, function(value, key) {
						var tempValue = {};
						tempValue = value;
						options[key] = tempValue;
					});
				}
				defer.resolve(options);
			});
			return defer.promise;
		}
		
		function initGeneral($scope, ctrl, updateProcessFlag) {
			var defer = $q.defer();
			ctrl.prodTypeOptions = [];
			ctrl.prodCodeOptions = [];
			//set prodcode Option
			ctrl.prodTypeChanged = function(value) {
				//add this for fixed wafencode bug
				var obj = {};
				if(ctrl.feeType) {
					obj.prodType = ctrl.feeType.prodType;
				}
				if(ctrl.searchCriteria) {
					obj.prodType = obj.prodType || ctrl.searchCriteria.prodType;
				}
				ajaxService.prodCodeOptions(obj).then(function(response) {
					ctrl.prodCodeOptions = response.data || [];
				});
			};
			dropDownDataOptions().then(function(response) {
				ctrl.yesOrNoOptions = response.yesOrNoOptions;
				ctrl.prodTypeClassOptions = response.prodTypeClassOptions;
				ctrl.prodTypeOptions = response.prodTypeOptions;
				ctrl.crDrOptions = response.crDrOptions;
				defer.resolve();
			}, angular.noop);
			return defer.promise;
		}
		return {
			dropDownDataOptions : dropDownDataOptions,
			initGeneral : initGeneral
		};
	}
})(angular);